package com.aydqyy.springbootstart.controller;

import com.aydqyy.springbootstart.dao.DeviceDao;
import com.aydqyy.springbootstart.dao.UserDao;
import com.aydqyy.springbootstart.entity.Device;
import com.aydqyy.springbootstart.service.DeviceService;
import com.aydqyy.springbootstart.utils.JwtUtil;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationContext;
import org.springframework.test.web.servlet.MockMvc;

import java.util.List;
import java.util.Map;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(controllers = DeviceController.class,
        excludeAutoConfiguration = {
                DataSourceAutoConfiguration.class,// 排除数据源)
                MybatisPlusAutoConfiguration.class,// 排除 MyBatis-Plus
                MybatisPlusLanguageDriverAutoConfiguration.class,
                SecurityAutoConfiguration.class  // 排除 Spring Security 自动配置
        }
)
@AutoConfigureMockMvc(addFilters = false)   // 禁用安全过滤器，包括拦截器，但是不包括自定义的拦截器

public class DeviceControllerTest2 {
    @MockBean
    private DeviceService deviceService;
    @MockBean
    private DeviceDao deviceDao;
 /*   @MockBean
    private UserDao userDao;*/
    @Autowired
    private MockMvc mockMvc;
    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void contextLoads() {
        // 验证上下文中是否存在 deviceController bean
        assertThat(applicationContext.containsBean("deviceController")).isTrue();
        // 获取 deviceController bean
        DeviceController deviceController = applicationContext.getBean(DeviceController.class);
        // 打印 deviceController 的类名
        System.out.println(deviceController.getClass());
    }

    @Test
    public void testList() throws Exception {
        // 生成 mockToken
        String mockToken = JwtUtil.genToken(Map.of("userId", 1, "name", "zwj"));
        // 生成 mockDevices
        List<Device> mockDevices = List.of(new Device(),new Device());
        // 当调用 deviceService.list() 方法时，返回 mockDevices
        when(deviceService.list()).thenReturn(mockDevices);
        // 发送 GET 请求，并添加 Token
        mockMvc.perform(get("/device/list")
                        .header("Authorization", mockToken))  // 添加 Token
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(0));
        verify(deviceService).list();
    }
}
